﻿
Public Class planificacion_data : Inherits Adaptador

    Event ErrorServer(ByVal Descripcion As String) 'evento de error que propaga el error de la clase adaptador (conexion a base de datos)
    Event GuardaCorrecto() 'evento propio de la clase, cuando guarda o realiza alguna operacion con exito.

    ''' <summary>
    ''' Devuelve todos los registros
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetData() As DataTable
        Conectar()
        Try
            Dim SQL As String
            SQL = "SELECT * FROM PLANIFICACION ORDER BY PLAN_ID"
            comando.CommandText = SQL
            comando.Connection = conn
            myReader = comando.ExecuteReader
            Dim dt As DataTable = Me.GetTable(myReader)
            Desconectar()
            Return dt
        Catch ex As Exception
            Desconectar()
            RaiseEvent ErrorServer(ex.Message)
        End Try
    End Function

    ''' <summary>
    ''' Buscar registros por COD/ID
    ''' </summary>
    ''' <param name="ID"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function msoBuscar(ByVal ID As String) As DataTable
        Conectar()
        Try
            Dim SQL As String
            SQL = "SELECT * FROM PLANIFICACION WHERE PLAN_ID = '" & ID & "'"
            comando.CommandText = SQL
            comando.Connection = conn
            myReader = comando.ExecuteReader
            Dim dt As DataTable = Me.GetTable(myReader)
            Desconectar()
            Return dt
        Catch ex As Exception
            Desconectar()
            RaiseEvent ErrorServer(ex.Message)
        End Try
    End Function


    ''' <summary>
    ''' guardar y actualizar
    ''' </summary>
    ''' <param name="ID"></param>
    ''' <param name="CURSO"></param>
    ''' <param name="UNIDAD_APREND"></param>
    ''' <param name="INDICADORES"></param>
    ''' <param name="APREND_ESPERADO"></param>
    ''' <param name="TEMA_CONTENIDO"></param>
    ''' <param name="ACTIV_DESARROLLO"></param>
    ''' <param name="ACTIV_TERMINO"></param>
    ''' <param name="PROCED_EVAL"></param>
    ''' <param name="BIBLIOGRAFIA"></param>
    ''' <param name="OBSERVACIONES"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function minGuarda(ByVal ID As String, _
                                        CLASE As Integer, _
                                    ByVal CURSO As String, _
                                    ByVal UNIDAD_APREND As String, _
                                    ByVal INDICADORES As String, _
                                    ByVal APREND_ESPERADO As String, _
                                    ByVal TEMA_CONTENIDO As String, _
                                    ByVal ACTIV_DESARROLLO As String, _
                                    ByVal ACTIV_TERMINO As String, _
                                    ByVal PROCED_EVAL As String, _
                                    ByVal BIBLIOGRAFIA As String, _
                                    ByVal OBSERVACIONES As String)

        Try
            Dim SQL As String
            Dim dt As DataTable = msoBuscar(ID)
            Conectar()
            If dt.Rows.Count = 0 Then

                SQL = "INSERT INTO PLANIFICACION(PLAN_ID, " _
                    & "PLAN_CLASE, " _
                    & "PLAN_CURSO, " _
                    & "PLAN_INDICADORES, " _
                    & "PLAN_APRENDI_ESPERADO, " _
                    & "PLAN_UNI_APRENDI, " _
                    & "PLAN_TEMA_CONTENIDO, " _
                    & "PLAN_ACTIV_DESARROLLO, " _
                    & "PLAN_ACTIV_TERMINO, " _
                    & "PLAN_PROCED_EVAL, " _
                    & "PLAN_BIBLIOGRAFIA, " _
                    & "PLAN_OBSERVACIONES) " _
                                         & "values(" _
                                         & "'" & ID & "'," _
                                         & "'" & CLASE & "'," _
                                         & "'" & CURSO & "'," _
                                         & "'" & INDICADORES & "'," _
                                         & "'" & UNIDAD_APREND & "'," _
                                         & "'" & APREND_ESPERADO & "'," _
                                         & "'" & TEMA_CONTENIDO & "'," _
                                         & "'" & ACTIV_DESARROLLO & "'," _
                                         & "'" & ACTIV_TERMINO & "'," _
                                         & "'" & PROCED_EVAL & "'," _
                                         & "'" & BIBLIOGRAFIA & "'," _
                                         & "'" & OBSERVACIONES & "')"
            Else
                SQL = "UPDATE PLANIFICACION SET " _
                                    & "PLAN_CLASE = '" & CLASE & "', " _
                                    & "PLAN_CURSO = '" & CURSO & "', " _
                                    & "PLAN_INDICADORES = '" & INDICADORES & "', " _
                                    & "PLAN_UNI_APRENDI = '" & UNIDAD_APREND & "', " _
                                    & "PLAN_APRENDI_ESPERADO = '" & APREND_ESPERADO & "', " _
                                    & "PLAN_TEMA_CONTENIDO = '" & TEMA_CONTENIDO & "', " _
                                    & "PLAN_ACTIV_DESARROLLO = '" & ACTIV_DESARROLLO & "', " _
                                    & "PLAN_ACTIV_TERMINO = '" & ACTIV_TERMINO & "', " _
                                    & "PLAN_PROCED_EVAL = '" & PROCED_EVAL & "', " _
                                    & "PLAN_BIBLIOGRAFIA = '" & BIBLIOGRAFIA & "', " _
                                    & "PLAN_OBSERVACIONES = '" & OBSERVACIONES & "' " _
                                    & " WHERE PLAN_ID = '" & ID & "'"
            End If
            comando.CommandText = SQL
            comando.Connection = conn
            comando.ExecuteNonQuery()
            Desconectar()
            RaiseEvent GuardaCorrecto()
        Catch ex As Exception
            Desconectar()
            RaiseEvent ErrorServer(ex.Message)
        End Try
    End Function

    ''' <summary>
    ''' Eliminar registro
    ''' </summary>
    ''' <param name="ID"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function mdnElimina(ByVal ID As String)

        Try
            Dim SQL As String
            Conectar()
            SQL = "DELETE FROM PLANIFICACION WHERE PLAN_ID = '" & ID & "'"
            comando.CommandText = SQL
            comando.Connection = conn
            comando.ExecuteNonQuery()
            Desconectar()
            RaiseEvent GuardaCorrecto()
        Catch ex As Exception
            Desconectar()
            RaiseEvent ErrorServer(ex.Message)
        End Try
    End Function


    ''' <summary>
    ''' Devuelve error especifico de la conexion (clase adaptador)
    ''' </summary>
    ''' <param name="Descripcion"></param>
    ''' <remarks></remarks>
    Private Sub Adaptador_ErrorBaseServer(ByVal Descripcion As String) Handles Me.ErrorBaseServer
        RaiseEvent ErrorServer(Descripcion)
    End Sub
End Class
